package com.mindmax.user.controller;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.mindmax.user.domain.Resource;
import com.mindmax.user.service.IResourceService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

/**
 * 亿图权限点Controller
 *
 * @author 胡瑞鹏
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/yt-resource")
@DataSource(value = DataSourceType.YT_ADMIN)
public class ResourceController extends BaseController
{
    @Autowired
    private IResourceService resourceService;



    @GetMapping("/treeselect")
    public AjaxResult treeselect()
    {
        List<Resource> menus = resourceService.selectResourceList(null);
        return success(resourceService.buildMenuTreeSelect(menus));
    }



    @GetMapping(value = "/roleMenuTreeselect/{tenantId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("tenantId") Long tenantId)
    {
        List<Resource> selectedMenus = resourceService.selectResourceListByTenantId(tenantId);
        List<Resource> allMenus = resourceService.selectResourceList(null);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", selectedMenus.stream().map(Resource::getId).collect(Collectors.toList()));
        ajax.put("menus", resourceService.buildMenuTreeSelect(allMenus));
        return ajax;
    }


    /**
     * 查询亿图权限点列表
     */
    @PreAuthorize("@ss.hasPermi('system:yt-resource:list')")
    @GetMapping("/list")
    public AjaxResult list(Resource resource)
    {
        List<Resource> list = resourceService.selectResourceList(resource);
        return success(list);
    }

    /**
     * 导出亿图权限点列表
     */
    @PreAuthorize("@ss.hasPermi('system:yt-resource:export')")
    @Log(title = "亿图权限点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Resource resource)
    {
        List<Resource> list = resourceService.selectResourceList(resource);
        ExcelUtil<Resource> util = new ExcelUtil<Resource>(Resource.class);
        util.exportExcel(response, list, "亿图权限点数据");
    }

    /**
     * 获取亿图权限点详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:yt-resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(resourceService.selectResourceById(id));
    }

    /**
     * 新增亿图权限点
     */
    @PreAuthorize("@ss.hasPermi('system:yt-resource:add')")
    @Log(title = "亿图权限点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Resource resource)
    {
        Long userId = getUserId();
        resource.setCreateUser(userId);
        resource.setUpdateUser(userId);
        Date nowDate = DateUtils.getNowDate();
        resource.setCreateTime(nowDate);
        resource.setUpdateTime(nowDate);
        return toAjax(resourceService.insertResource(resource));
    }

    /**
     * 修改亿图权限点
     */
    @PreAuthorize("@ss.hasPermi('system:yt-resource:edit')")
    @Log(title = "亿图权限点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Resource resource)
    {
        Long userId = getUserId();
        resource.setUpdateUser(userId);
        Date nowDate = DateUtils.getNowDate();
        resource.setUpdateTime(nowDate);
        return toAjax(resourceService.updateResource(resource));
    }

    /**
     * 删除亿图权限点
     */
    @PreAuthorize("@ss.hasPermi('system:yt-resource:remove')")
    @Log(title = "亿图权限点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(resourceService.deleteResourceByIds(ids));
    }
}
